package com.ylsk.check.service.impl;

import java.util.Date;
import java.util.List;

import com.google.common.primitives.Longs;
import com.ylsk.check.domain.ReportInfo;
import com.ylsk.check.domain.dto.JeevesReportDTO;
import com.ylsk.check.domain.dto.MsgTemplateDTO;
import com.ylsk.check.domain.emum.BasicInfoTemplateEnum;
import com.ylsk.check.domain.emum.ConstructionStatusEnum;
import com.ylsk.check.domain.emum.MsgTemplateEnum;
import com.ylsk.check.domain.so.JeevesReportSO;
import com.ylsk.check.domain.vo.JeevesReportVO;
import com.ylsk.common.annotation.DataScope;
import com.ylsk.common.config.SnowIdConfig;
import com.ylsk.common.exception.ServiceException;
import com.ylsk.common.utils.DateUtils;
import com.ylsk.common.utils.Generic;
import com.ylsk.common.utils.SecurityUtils;
import com.ylsk.common.utils.SerialNoUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ylsk.check.mapper.JeevesReportMapper;
import com.ylsk.check.domain.JeevesReport;
import com.ylsk.check.service.IJeevesReportService;

/**
 * 占道上报Service业务层处理
 *
 * @author yangsy
 * @date 2024-01-02
 */
@Service
public class JeevesReportServiceImpl implements IJeevesReportService
{
    @Autowired
    private JeevesReportMapper jeevesReportMapper;

    @Autowired
    private SnowIdConfig snowIdConfig;

    @Autowired
    private SerialNoUtils serialNoUtils;

    @Autowired
    private ReportInfoHelper helper;

    /**
     * 查询占道上报
     *
     * @param jeevesReportId 占道上报主键
     * @return 占道上报
     */
    @Override
    public JeevesReportVO selectJeevesReportByJeevesReportId(Long jeevesReportId)
    {
        return jeevesReportMapper.selectJeevesReportByJeevesReportId(jeevesReportId);
    }

    /**
     * 查询占道上报列表
     *
     * @param jeevesReportSO 占道上报
     * @return 占道上报
     */
    @Override
    @DataScope(deptAlias = "sd",userAlias = "su")
    public List<JeevesReportVO> selectJeevesReportList(JeevesReportSO jeevesReportSO)
    {
        return jeevesReportMapper.selectJeevesReportList(jeevesReportSO);
    }

    /**
     * 新增占道上报
     *
     * @param jeevesReportDTO 占道上报
     * @return 结果
     */
    @Override
    public int insertJeevesReport(JeevesReportDTO jeevesReportDTO)
    {
        JeevesReport jeevesReport = new JeevesReport();
        BeanUtils.copyProperties(jeevesReportDTO,jeevesReport);
        final Long jeevesReportId = snowIdConfig.getSnowId();
        jeevesReport.setJeevesReportId(jeevesReportId);
        //  占道报备流水号
        jeevesReport.setJeevesNum(serialNoUtils.generateSerialNo(SerialNoUtils.ZDBB_ORDER));

        ReportInfo reportInfo = helper.getReportInfo(jeevesReport.getReportInfoId());
        helper.checkData(reportInfo,new Generic<>(jeevesReportDTO));

        //  施工状态  默认为非占道施工
        ConstructionStatusEnum constructionStatusEnum = ConstructionStatusEnum.CONSTRUCTION;
        //设置状态 如果当前时间 大于占道开始日期 则设置为 为占道施工 反之设置为非占道施工
        if(new Date().after(jeevesReportDTO.getJeevesStartDate())){
            constructionStatusEnum = ConstructionStatusEnum.ROAD_CONSTRUCTION;
            //  生成检查单
            helper.saveTask(reportInfo.getReportInfoId(),constructionStatusEnum.getCode(), BasicInfoTemplateEnum.INS_JEEVES_REPORT.getTbName(),jeevesReportId,jeevesReportDTO.getJeevesStartDate(),reportInfo.getDeptId());
        }else{
            //  判断当前项目占道列表，是否有未提交撤场上报的单子，并且当前时间大于占道上报起点时间
            List<JeevesReportVO> jeevesReportVOList = jeevesReportMapper.selectNotSubmitWithdrawalJeevesReportList(new JeevesReportSO(reportInfo.getReportInfoId()));
            for (JeevesReportVO jeevesReportVO : jeevesReportVOList) {
                if(new Date().after(jeevesReportVO.getJeevesStartDate())){
                    constructionStatusEnum = constructionStatusEnum.ROAD_CONSTRUCTION;
                    break;
                }
            }
        }
        //  更新施工状态
        helper.updateReportConstructionStatus(reportInfo,constructionStatusEnum);
        MsgTemplateDTO msgTemplateDTO = new MsgTemplateDTO(MsgTemplateEnum.TEMPLATE_ZDSB,SecurityUtils.getUserId(), Longs.asList(reportInfo.getReviewedBy()));
        String p1 = SecurityUtils.getLoginUser().getUser().getNickName();
        String p2 = DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss");
        String p3 = reportInfo.getProjectName();
        String p4 = DateUtils.dateTime(jeevesReport.getJeevesStartDate()) + "至" + DateUtils.dateTime(jeevesReport.getJeevesEndDate());
        helper.saveMsg(msgTemplateDTO,p1,p2,p3,p4);

        jeevesReport.setDeptId(reportInfo.getDeptId());
        jeevesReport.setReviewedBy(reportInfo.getReviewedBy());
        return jeevesReportMapper.insert(jeevesReport);
    }

    /**
     * 修改占道上报
     *
     * @param jeevesReportDTO 占道上报
     * @return 结果
     */
    @Override
    public int updateJeevesReport(JeevesReportDTO jeevesReportDTO)
    {
        final Long jeevesReportId = jeevesReportDTO.getJeevesReportId();
        if(null == jeevesReportId){
            throw new ServiceException("占道上报id为空!");
        }
        JeevesReport jeevesReport = new JeevesReport();
        BeanUtils.copyProperties(jeevesReportDTO,jeevesReport);
        return jeevesReportMapper.updateById(jeevesReport);
    }

    /**
     * 批量删除占道上报
     *
     * @param jeevesReportIds 需要删除的占道上报主键
     * @return 结果
     */
    @Override
    public int deleteJeevesReportByJeevesReportIds(Long[] jeevesReportIds)
    {
        return jeevesReportMapper.deleteJeevesReportByJeevesReportIds(jeevesReportIds);
    }

    /**
     * 删除占道上报信息
     *
     * @param jeevesReportId 占道上报主键
     * @return 结果
     */
    @Override
    public int deleteJeevesReportByJeevesReportId(Long jeevesReportId)
    {
        return jeevesReportMapper.deleteJeevesReportByJeevesReportId(jeevesReportId);
    }

    @Override
    public List<JeevesReportVO> selectJeevesByReportInfoId(List<Long> ids) {
        return Lists.newArrayList();
    }

    @Override
    public List<JeevesReportVO> selectJeevesNotWithdrawnList(JeevesReportSO jeevesReportSO) {
        if(jeevesReportSO == null ||jeevesReportSO.getReportInfoId() == null){
            throw new ServiceException("项目报备id不能为空!");
        }
        return jeevesReportMapper.selectJeevesNotWithdrawnList(jeevesReportSO);
    }

    @Override
    public List<JeevesReportVO> getTodayJeevesReport(JeevesReportSO jeevesReportSO) {
        return jeevesReportMapper.getTodayJeevesReport(jeevesReportSO);
    }
}
